package com.aibili.contoller.administrator;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.aibili.contoller.BaseController;
import com.aibili.pojo.ChargeCard;
import com.aibili.pojo.GiftInfo;
import com.aibili.pojo.model.PageBodyModel;
import com.aibili.pojo.model.PageModel;
import com.aibili.service.FileService;
import com.aibili.service.GiftService;
import com.alibaba.fastjson.JSON;
import com.daoshun.common.CommonUtils;
import com.daoshun.common.DsConstant;

@Controller
@RequestMapping("/administrator")
public class GiftController extends BaseController {

	@Resource
	private GiftService giftService;

	@Resource
	private FileService fileService;

	/**
	 * 礼物列表
	 * 
	 * @param pageIndex
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("giftinfo")
	public ModelAndView giftinfo(Integer pageIndex) {
		ModelAndView mav = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		PageBodyModel<GiftInfo> results = giftService.getByParams(pageIndex, DsConstant.PAGESIZE);
		if (results != null) {
			List<GiftInfo> gifts = results.getList();
			PageModel page = results.getPageModel();
			mav.addObject("list", gifts);
			mav.addObject("page", page);
		}

		return mav;
	}

	/**
	 * 添加或编辑礼物
	 * 
	 * @param id
	 * @param gift_name
	 * @param imgsfile
	 * @param point
	 * @param time
	 * @return
	 */
	@RequestMapping("giftedit")
	public ModelAndView giftedit(String id, String gift_name, @RequestParam("imgsfile") MultipartFile imgsfile,
			Integer pageIndex) {
		ModelAndView mov = new ModelAndView();

		if (pageIndex == null || pageIndex <= 0) {
			pageIndex = 1;
		}

		GiftInfo info = giftService.getGiftInfo(CommonUtils.parseLong(id, 0));
		if (imgsfile != null && !imgsfile.isEmpty()) {
			String filepath = fileService.saveFile(imgsfile);
			info.setPic(filepath);
		}
		info.setGift_name(gift_name);
		giftService.updateGiftInfo(info);

		mov.addObject("pageIndex", pageIndex);
		mov.setViewName("redirect:/administrator/giftinfo");
		return mov;
	}

	/**
	 * 
	 * @Description 获取充值卡信息
	 * @return
	 */
	@RequestMapping("chargecardinfo")
	public ModelAndView chargecardinfo() {
		ModelAndView mav = new ModelAndView();
		List<ChargeCard> list = giftService.getChargeCard();
		mav.addObject("list", list);
		return mav;
	}

	/**
	 * 
	 * @Description 充值卡修改
	 * @param id
	 * @param point
	 * @param money
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/chargecardedit", produces = "application/json; charset=UTF-8")
	public String chargecardedit(String id, int point, int money) {
		Map<String, Object> map = new HashMap<String, Object>();
		if (!CommonUtils.isEmptyString(id)) { // 修改
			long uid = CommonUtils.parseLong(id, 0);
			ChargeCard card = giftService.getChargeCardById(uid);
			if (card != null) {
				card.setIs_delete(1);
				giftService.updateChargeCard(card);

				ChargeCard card1 = new ChargeCard();
				card1.setIs_delete(0);
				card1.setMoney(money);
				card1.setPoint(point);
				card1.setCreate_time(new Date());
				
				giftService.addChargeCard(card1);
				map.put("code", 0);
			} else {
				map.put("code", 1);
			}
		} else {
			map.put("code", 1);
		}

		return JSON.toJSONString(map);
	}

}
